Part Number Hot Search : 
7824CT SRA2203N CPH6424 VC2G0 GP4065D APTGT1 AZ23C3V0 IN74HC86
Product Description
Full Text Search
 

To Download AN2284 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  rev 1.0 AN2284/0106 1/40 40 AN2284 application note smart card interface with the str71xx introduction this document describes a software and hardware smart card interface for the str71x smart card peripheral. the main purpose of this software and hardware package is to provide resources facilitating the development of an application using the smart card peripheral. the software interface is composed of library source files and some application template source files. www.st.com
AN2284 2/40 contents 1 file organization of the smart card library . . . . . . . . . . . . . . . . . . . . . . . 6 2 smart card reader using str710: overview . . . . . . . . . . . . . . . . . . . . . . . 7 3 device description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.1 str710 microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4 sc peripheral description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.1 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.2 external interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.3 protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.4 smart card clock generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 5 sc reader: basic hw for 5v card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 6 sc iso7816: protocol overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 6.1 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 6.2 iso 7816-2 - pins location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 6.2.1 pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7 iso 7816-3 - electronic signal and transmission protocol . . . . . . . . . . . 15 7.0.1 card power-on and reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 7.0.2 data transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 7.0.3 answer to reset: atr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 7.1 iso 7816-4 - smart card commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 7.1.1 the t0 protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 7.2 application level protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 7.2.1 the iso 7816-4 apdu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 7.2.2 the file system api . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 7.2.3 the iso 7816-4 functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 7.2.4 the security api . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 8 sc reader lib: description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8.1 sc parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
AN2284 3/40 8.1.1 sc_action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8.1.2 sc_state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8.1.3 cmdstatus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 8.2 how to send adpu commands to sc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 8.2.1 sc_select_file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8.2.2 sc_get_a2r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8.2.3 sc_get_responce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8.2.4 sc_read_binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 8.2.5 sc_create_file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 8.2.6 sc_update_binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 8.2.7 sc_verify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 9 sc reader lib: parity error management . . . . . . . . . . . . . . . . . . . . . . . . . 34 9.1 data sent from card to reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 9.2 data sent from reader to card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 10 sc reader example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 10.1 firmware package description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 10.2 firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 10.2.1 smart card start up: answer to reset (a2r) . . . . . . . . . . . . . . . . . . . . . . . . 36 10.2.2 reading a file at aspecified path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 10.2.3 writing a file at a specified path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 10.2.4 creating a file at a specified path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 10.2.5 creating a file at a specified path with pin protection . . . . . . . . . . . . . . . . 37 11 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
AN2284 4/40 figures figure 1. iso 7816-3 asynchronous protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 figure 2. basic hardware for 5v card interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 figure 3. contact definitions for smart cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 figure 4. reader and card fsm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 figure 5. answer to reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 figure 6. application communication architecture. . . . . . . . . . . . . . . . . . . . . . . . . . 21 figure 7. the command apdu structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 figure 8. the response apdu structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 figure 9. the smart card file system architecture. . . . . . . . . . . . . . . . . . . . . . . . . 23 figure 10. state machine for smart card operation . . . . . . . . . . . . . . . . . . . . . . . . . 30 figure 11. firmware package: directory tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 figure 12. smart card example: file system description . . . . . . . . . . . . . . . . . . . . . 36
AN2284 5/40 tables table 1. file library description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 table 2. smart card pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 table 3. pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 table 4. the answer-to-reset structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 table 5. cla instruction set definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 table 6. iso 7816-4 ins codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 table 7. firmware package: directories description . . . . . . . . . . . . . . . . . . . . . . . 35
1 file organization of the smart card library AN2284 6/40 1 file organization of the smart card library the following table presents the library modules: table 1. file library description file description device_scr.h smart card definitions, types definitions and function prototypes device_scr.c t0 protocol management sc.h,sc.c physical layer
AN2284 2 smart card reader using str710: overview 7/40 2 smart card reader using str710: overview a smart card reader was developed using the str710 arm7tdmi powered microcontroller and a basic hw to interface 5v powered smart card. the smart card library was developed in order to support iso7816-3/4 specification.
3 device description AN2284 8/40 3 device description 3.1 str710 microcontroller here is a brief description of the str710 microcontroller. please refer to the str710 datasheet for a detailed description. memories - 272kb (256+16k) flash program memory ( 100,000 cycles endurance, data retention 20 years) - 64kb ram - external memory interface (emi) for up to 4 banks of sram, flash, rom - multi-boot capability clock, reset and supply management - 3.3v application supply and i/o interface - embedded 1.8v voltage regulator for core supply - 0 to 16mhz external main oscillator - 32khz external backup oscillator - internal pll for cpu clock - up to 50mhz cpu operating frequency when executing from flash - realtime clock for clock-calendar function - 4 power saving modes: slow, wait, stop and standby modes nested interrupt controller - fast interrupt handling with multiple vectors - 32 vectors with 16 irq priority levels - 2 maskable fiq sources up to 48 i/o ports - 30/32/48 multifunctional bidirectional i/o - 14 ports with interrupt capability 5 timers - 16-bit watchdog timer four 16-bit timers each with: 2 input captures, 2 output compares, pwm and pulse counter 10 communications interfaces - 2 i2c interfaces (1 multiplexed with spi) - 4 uart asynchronous serial communications interfaces - smart card iso7816-3 interface on uart1 - 2 bspi synchronous serial interfaces - can interface (2.0b active)
AN2284 3 device description 9/40 - usb v 2.0 full speed (12mbit/s) device function with suspend and resume support - hdlc synchronous communications interface 4-channel 12-bit a/d converter - conversion time: - 4 channels: up to 500hz (2 ms) - 1 channel: up to 1khz (1 ms) - conversion range: 0 to 2.5v development tools support - jtag with debug mode trigger request
4 sc peripheral description AN2284 10/40 4 sc peripheral description 4.1 introduction the smartcard interface is an extension of uart1; for the description of the uart registers, please refer to str710 datasheet. the smartcard interface is designed to support asynchronous protocol smartcards as defined in the iso7816-3 standard. with smartcard mode enabled, uart1 is configured as: - eight data bits plus parity - 0.5 or 1.5 stop bits a 16 bit counter and the smartcard clock generator provide the clock to the smartcard. gpio bits in conjunction with software are used to provide the rest of the functions required to interface to the smartcard. the inverse signalling convention as defined in iso7816-3, inverted data and msb first, is handled in the software. 4.2 external interface table 2. smart card pins the scrst, sccmdvpp (command for vpp), sccmdvcc (command for vcc), and scdetect signals (signal for card detection) are provided by gpio bits of the io ports under software control. programming the gpio bits of the port for alternate function modes connects the uart txd data signal to the scdataout pin with the correct driver type and the clock generator to the scclk pin. 4.3 protocol the iso standard defines the bit times for the asynchronous protocol in terms of a time unit called an etu which is related to the clock frequency input to the card. one bit time is of length one etu. the uart transmitter output and receiver input need to be connected together externally. for the transmission of data from the str71x to the smartcard, the uart will need to be set up in smartcard mode. pin number pin name function p0.12 sccik smart card clock p0.10 i/o i/o serial data: open drain @ both ends any gpio rst reset to card vcc supply voltage vpp programming voltage
AN2284 4 sc peripheral description 11/40 figure 1. iso 7816-3 asynchronous protocol 4.4 smart card clock generator the smartcard clock generator provides a clock signal to the connected smartcard. the smartcard uses this clock to derive the baud rate clock for the serial i/o between the smartcard and another uart. the clock is also used for the cpu in the card, if present. operation of the smart card interface requires that the clock rate to the card is adjusted while the cpu in the card is running code so that the baud rate can be changed or the performance of the card can be increased. the protocols that govern the negotiation of these clock rates and the altering of the clock rate are detailed in iso7816-3 standard. the clock is used as the cpu clock for the smart card, therefore updates to the microcontroller clock rate must be synchronized to the smartcard clock, i.e. the clock high or low pulse widths must not be shorter than either the old or new programmed value.
5 sc reader: basic hw for 5v card AN2284 12/40 5 sc reader: basic hw for 5v card some adjustments are needed when using a 5v powered smart card. figure 2. shows how to interface a 5v smart card with the microcontroller (str710): figure 2. basic hardware for 5v card interface
AN2284 6 sc iso7816: protocol overview 13/40 6 sc iso7816: protocol overview 6.1 introduction "iso7816: identification cards integrated circuit(s) cards with contacts" provides the basis to transition the relatively simple identification card from a token that can be compromised through forgery, theft, or loss into a tamper-resistant and "intelligent" integrated circuit card (icc), more popularly known as a smart card. iso 7816 includes at least six approved parts and has several additional parts under review: part 1, physical characteristics part 2, dimensions and location of the contacts part 3, electronic signals and transmission protocols part 3, amendment 2-revision of protocol type selection part 4, inter-industry commands for interchange part 5, numbering system and registration procedure for application identifiers 6.2 iso 7816-2 - pins location iso 7816-2 specifies an icc with eight electrical contacts present in a standardized position on the front face of the card. these are referred to as c1 through c8. some of these contacts are electrically connected to the microprocessor chip embedded within the card; some are not, having been defined to allow for enhancements but unused at the present time. the picture below depicts the contacts positions. figure 3. contact definitions for smart cards
6 sc iso7816: protocol overview AN2284 14/40 6.2.1 pin assignment table 3. pin assignment c1: vcc=5v c5:gnd c2: reset c6: vpp c3: clock c7: i/o c4: rfu c8: rfu
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 15/40 7 iso 7816-3 - electronic signal and transmission protocol iso 7816-3 begins to delve into the specification of the "intelligent" aspects of the smart card. this standard describes the relationship between the smart card and the reader as one of "slave" (the smart card) and "master" (the reader). communications are established by the reader signaling to the smart card through the contacts noted previously and are continued by the smart card responding accordingly. communication between the card and reader proceed according to various state transitions illustrated in figure 3. . figure 4. reader and card fsm the communication channel is single-threaded; once the reader sends a command to the smart card, it blocks until a response is received.
7 iso 7816-3 - electronic signal and transmission protocol AN2284 16/40 7.0.1 card power-on and reset when a card is inserted into a reader, no power is applied to any of the contacts. the chip on the card could be seriously damaged by applying power to the wrong contacts, and this situation could easily occur if a card were inserted across powered contact points. the contacts remain unpowered until an edge detector determines that the card is properly aligned with the contact points to within some acceptable (for the reader) mechanical tolerance. when the reader detects that the card is properly inserted, power is applied to the card. first, the contacts are brought to a coherent idle state, as shown in table 3.4. a reset signal is then sent to the card via the rst contact line. the idle state occurs when the power (vcc) contact is brought up to a normal, stable operating voltage of 5v. an initial power setting of 5v is always applied first, even though some microprocessor chips being introduced operate at 3v when in an i/o state. the i/o contact is set to a reception mode on the reader side and a stable clock (clk) is applied. the reset line is in a low state. it must remain in a low state for at least 40,000 clk cycles before a valid reset sequence can be started by the reader, raising the reset line to a high state. figure 5. answer to reset 7.0.2 data transfer data transfer between the reader and the card occurs through the concerted action of two of the contact lines: clk and i/o. the i/o line conveys a single bit of information per unit of time as defined by the clk depending on its voltage relative to gnd. a 1 bit can be conveyed either through a +5v value or through a 0v value. the actual convention used is determined by the card and is conveyed to the reader through the "initial character" of the atr, which is referenced as ts. to transfer 1 byte of information, 10 bits are actually moved across the i/o line; the first is always a "start bit" and the last is always a parity bit used to convey even parity. considering that the i/o line can be (in one bit period) either in a high (h) state or a low (l) state, the ts character of the form hlhhllllllh signals that the card wants to use the "inverse convention," meaning that h corresponds to a 0 and l corresponds to a 1. a ts
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 17/40 character of the form hlhhlhhhllh signals that the card wants to use the "direct convention," meaning that h corresponds to a 1 and l corresponds to a 0. the direct convention and the inverse convention also control the bit ordering with each byte transferred between the card and the reader. in the direct convention, the first bit following the start bit is the low-order bit of the byte. successively higher-order bits follow in sequence. in the inverse convention, the first bit following the start bit is the high-order bit of the byte. successively lower-order bits follow in sequence. parity for each byte transferred should be even; this means that the total number of 1 bits in the byte, including the parity bit, must be an even number. the i/o line comprises a half-duplex channel; that is, either the card or the reader can transmit data over the same channel, but they both cannot be transmitting at the same time. so as part of the power-up sequence, both the reader and the card enter a receive state in which they are listening to the i/o line. with the commencement of the reset operation, the reader remains in the receive state while the card must enter a send state in order to send the atr back to the reader. from this point on, the two ends of the channel alternate between send states and receive states. with a half-duplex channel, there is no reliable way for either end to asynchronously change a state from send to receive or from receive to send. rather, if this is desired, that end must go into a receive state and allow a time-out of the operation in progress; then a reader end w ill always try to re-establish a known s equence by re-entering a send state. the clk and i/o lines can support a wide variety of data transmission speeds. the specific speed is defined by the card and is conveyed back to the reader through an optional character in the atr. the transmission speed is set through the establishment of one bit time on the i/o line, which means that an interval is established at which the i/o line may be sampled in order to read a bit and then each successive bit. this time is defined as an elementary time unit (etu) and is established through a linear relationship between several factors. note that the ts character was returned before any definition of the etu could be made. this is possible because the etu during the atr sequence is always specified to be etu 0 =372/(clk frequency) where the clk frequency is always between 1mhz and 5 mhz; in fact, the frequency is almost always selected such that the initial data transfer rate is 9,600 bits per second. 7.0.3 answer to reset: atr once an rst signal is sent from the reader to the card, the card must respond with the first character of the atr within 40,000 clk cycles. the card might not respond with an atr for a number of reasons, the most prevalent being that the card is inserted incorrectly into the reader (probably upside down). in some instances, the card may not be functioning because it has been damaged or broken. whatever the case, if the atr is not returned within the prescribed time, the reader should begin a sequence to power down the card. during this sequence, the reader sets the rst, clk, and i/o lines low and drops voltage on the vcc line to nominal 0 (that is, less than 0.4v). the atr is a string of characters returned from the card to the reader following the successful completion of the power-up sequence. as defined in iso/iec 7816-3, the atr consists of 33 or fewer characters comprising the following elements: ts - a mandatory initial character t0 - a mandatory format character tai tbi tci tdi-optional interface characters t1, t2,tk-optional historical characters tck - a conditional check character
7 iso 7816-3 - electronic signal and transmission protocol AN2284 18/40 the historical characters can be defined at the discretion of the card manufacturer or the card issuer. these characters are typically used to convey some type of designation of the type, model, and use of this specific card. when used in this way, the historical characters provide a modest mechanism through which systems can automatically detect the use of an inserted card (within that system) and can initiate other actions (or software) accordingly. the check character provides a mechanism through which the integrity of the atr can be measured; that is, whether a transmission error has occurred in sending the characters from the card to the reader. the structure of the atr is illustrated in table 4. as discussed previously, the initial ts character is used to establish the bit-signaling and bit-ordering conventions between the reader and the card. the t0 character is used to signal the presence or absence of subsequent interface characters or historical characters. the interface characters are used to tailor the characteristics of the i/o channel, including the specific protocol used by the card and reader during subsequent exchange of commands (from the reader to the card) and responses (from the card to the reader). the historical characters, if present, are used to convey card- manufacturer-specific information from the card to the read, and hence to the application system being served by the reader . there is really no established standard for the information presented in the historical bits. the total length of the atr sequence is limited to 33 bytes and must adhere to the following format: table 4. the answer-to-reset structure character id definition initial character section ts mandatory initial character format character section t0 indicator for presence of interface characters interface character section ta 1 global, codes f1 and d1 tb 1 global, codes 11 and pl1 tc 1 global, code n td 1 codes y 2 and t ta 2 specific tb 2 global, code pl2 tc 2 specific td 2 codes y 3 and t ta 3 ta i , tb i and tc i are specific ...td i codes y i+1 and t historical character section t1 card specific information ...tk (maximum of 15 characters) check character section tck 2 optional check character
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 19/40 7.1 iso 7816-4 - smart card commands the previous chapter discussed the answer-to-reset (atr) mechanism, which establishes a basic communication channel between the smart card and the reader. this channel is a half- duplex physical channel. this chapter investigates the use of more complex protocols on top of this physical channel. a link-level communication protocol resides directly on top of the physical channel, providing error-free communication between the reader and the smart card. once this link level protocol is established, application-level protocols can be defined. iso 7816-4 defines two such application-level protocols: file system api providing a set of functions to manipulate files (e.g. read, write, select etc.) security service api allowing the smart card and the reader to mutually authenticate themselves and also to encrypt data to be exchanged between the card and the reader. iso 7816-4 defines a protocol message structure to support the application protocol apis. this message structure consists of application protocol data units (apdus) which are exchanged between the reader application and the smart card application by the link level protocol. this chapter will provide an overview of the file access and security apis. a more detailed description can be found in appendix a, "the iso 7816-4 command set". 7.1.1 the t0 protocol the t=0 protocol is a byte-oriented protocol where a character is transmitted across the channel between the reader and the card. in addition, error handling is performed on each byte by looking at the parity bit. if the actual parity bit does not correspond to the parity of the transmitted data, then an error must have occurred. in the t=0 protocol, the receiving side signals that it requires the byte to be retransmitted in the case of detecting a parity error. this is done by holding the i/o line low (normally the i/o line is set high preceding the transfer of a byte). when the transmitting side detects this, it resends the byte that was previously received in error. the reader and the smart card exchange data structures known as transmission protocol data units (tpdus). it consists of two distinct structures: a command that is sent from the reader to the card a response that is sent from the card to the reader the command header includes the following five fields each of one byte in length: cla: class designation of the command set to establish a collection of instructions ins: specifies a specific instruction from within the set of instructions p1: used to specify the addressing used by the [cla, ins] instruction p2: also used to specify the addressing used by the [cla, ins] instruction p3: specifies the number of data bytes transferred to or from the card as part of the [cla, ins] instruction execution. each value of cla defines an application-specific set of instructions. table 5. below lists values for some sets of instructions.
7 iso 7816-3 - electronic signal and transmission protocol AN2284 20/40 table 5. cla instruction set definitions the ins byte is used to identify a specific instruction within a class of instructions identified by the value of cla. table 6. lists the instructions in the iso 7816-4 standard used to access file system and security functions. table 6. iso 7816-4 ins codes the parameters p1 and p2 are defined at the link level but are actually dependent on the specific instruction (application level). they provide control or addressing parameters for the various application-specific instructions. for example, in the select file instruction, p1 is used to indicate how the file will be referred to (by identifier, name, path etc.) and p2 offers further refinement as to which file is to be selected. p3 defines the number of bytes to be transmitted during the execution of the ins specified instruction. the convention used to indicate movement of data is card-centric i.e. outgoing refers to data moving from the card to the reader and incoming refers to data moving from the reader to the card. for each command tdpu sent from the reader, a response tpdu is sent by the card. the response includes three mandatory fields and one optional field (all one byte in length): ack: indicates that the card has received the [cla, ins] command null: used for flow control on the i/o channel by the card. it signals (to the reader) that the card is still processing the command and so the reader must wait before sending another command sw1: status response of the current command sw2: (optional) also conveys a status response to the reader cla byte instruction set 0x iso 7816-4 instructions (files and security) 10 to 7f reserved for future use 8x or iso 7816-4 instructions ax application/vendor specific instructions b0 to cf iso 7816-4 instructions d0 to fe application/vendor specific instructions ff reserved for protocol type selection ins value command name ins value command name 0e erase binary c0 get response 20 verify c2 envelope 70 manage channel ca get data 82 external authenticate d0 write binary 84 get challenge d2 write record 88 internal authenticate d6 update binary a4 select file da put data b0 read binary dc update record b2 read record(s) e2 append record
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 21/40 the ack byte is a repeat of the ins byte from the command tpdu. if the response does not reach the reader within a specified time, the reader may initiate a rst sequence to restart the protocol between the reader and the card. this can be prevented if the reader receives at least one null byte from the card. sw1 informs the reader of the result of the requested instruction. the values allowed for sw1 are defined as part of the application protocol. some instructions require the card to send data to the reader. in this case sw2 is returned to the reader, triggering the reader to execute a getresponse command. the card will then return the data bytes generated by the execution of the previous command. 7.2 application level protocols the iso 7816-4 standard addresses two areas of functionality for application software: file system: a set of functions is provided in the form of an api. by using this api application software on the reader side can access files in the file system. security functions: these can be used to limit access to application software or to files on the card. the t=0 protocol is used to support application-level protocols between the smart card application and the reader application. these application protocols exchange data structures called application protocol data units (apdus). the following diagram illustrates this architecture: figure 6. application communication architecture the apdu structure defined by iso 7816-4 is very similar to the tpdu structure used in the t=0 protocol. in fact, when an apdu is transported by the t=0 protocol, the elements of the apdu directly overlay the elements of the tpdu.
7 iso 7816-3 - electronic signal and transmission protocol AN2284 22/40 7.2.1 the iso 7816-4 apdu there are two types of messages used to support the iso 7816-4 application protocols: the command apdu (sent from the reader to the card) and the response apdu (sent from the card to the reader). figure 7. the command apdu structure the command apdu consists of a header and a body (this can be seen in the diagram above). the header includes cla, ins, p1 and p2 fields. as in the t=0 protocol, cla and ins specify an application class and instruction. p1 and p2 are used to qualify specific instructions and are given specific definitions by each [cla, ins] instruction. the body of the apdu can vary in size and is used to transmit data to the card's apdu processor as part of a command or to convey a response from the card to the reader. the lc field specifies the number of bytes to be transmitted to the card as part of the instruction i.e. the length of the data field. the data field contains information that must be sent to the card to allow its apdu processor to execute the command specified in the apdu. the le field specifies the number of bytes t hat will be returned to the reader in the response apdu. the body of the apdu can take on four different forms: case 1: no data is transferred to or from the card, so the apdu only contains the header. case 2: no data is transferred to the card, but data is returned from the card. the body of the apdu only contains a non-null le field. case 3: data is transferred to the card, but none is returned from it. the body of the apdu includes the lc and data fields. case 4: data is transferred to the card and is also returned from the card as a result of the command. the body of the apdu includes the lc, data and le fields. figure 8. the response apdu structure the response apdu has a much simpler structure than that of the command apdu. it consists of a body and a trailer. the body is either null or it includes a data field - depending on the specific command. the length of the data field is determined by the le field in the corresponding command apdu. the trailer consists of up to two fields of status information
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 23/40 called sw1 and sw2. these fields return a status code in which one byte is used to specify an error category and the other is used to specify a command-specific status or error indication. 7.2.2 the file system api the file system is used on the nonvolatile memory or eeprom. it is defined as a simple hierarchical structure (similar to conventional file systems). the file system may contain three types of files (identified by a 2-byte identifier): master file (mf) dedicated file (df) elementary file (ef) there is one master file found on each smart card and it is the root of the file system. a master file may contain dedicated files or elementary files. the file identifier reserved for the master file is 3f00. the directory file is essentially a container (or directory) for elementary files - a df may contain zero or more efs. dedicated files partition the smart card into an ordered structure of elementary files. a dedicated file must be given a unique file identifier within the dedicated file or master file that contains it - allowing for a unique path for each file. a dedicated file can also be referenced by name (1-16 bytes long). the naming convention can be found in iso 7816-5. an elementary file is a leaf node in the hierarchy and contains the actual data. an elementary file can be identified by a 5-bit identifier with in a dedicated file. this file system hierarchy is illustrated in figure 9. figure 9. the smart card file system architecture there are four types of elementary files: transparent file linear, fixed-length record file linear, variable-length record file cyclic, fixed-length record file a transparent file is essentially a string of bytes i.e. an unstructured binary file. consequently a byte offset from the start of the file is required when data is to be read or written to this type of file. in addition, a command to read or write to a transparent file will contain the length of the byte string to be read or written to the file.
7 iso 7816-3 - electronic signal and transmission protocol AN2284 24/40 fixed and variable length files contain records that are identified by a sequence number. in a fixed-length record file, all the records contain the same number of bytes. in contrast, a variable-length record file contains records that may vary in length. as a result, variable-length record files have a higher overhead in read/write access times as well as a higher administrative overhead required by the file system. a cyclic file allows applications to access records in a consistent and transparent manner. it can be thought of as a ring of files. write operations are performed on the next physical record in the ring. 7.2.3 the iso 7816-4 functions the few functions that are defined in iso 7816-4 for selecting, reading and writing to files will be briefly discussed below. select file: this command establishes a logical pointer to a particular file in the smart card's file system. this pointer is required for any file manipulation operation. access to the smart card's file system is not multithreaded, however it is possible to have several file pointers defined at any point in time. this is accomplished by the manage channel command, which establishes multiple logical channels between the reader side application and the card. this allows different files on the card to be in various states of access by the reader application at the same time. the identification of the file can be provided in the following ways: file identifier (2-byte value) df name (string of bytes) path (concatenation of file identifiers) short id note that not all smart cards support all four naming mechanisms. read binary: this command is used by the application on the reader side to retrieve a part of an ef on the card. however, the ef must be a transparent file (not record-oriented). if the read binary command is attempted on a record-oriented ef, the command will abort with an error indicator being returned from the card. the read binary command takes two parameters: an offset pointer from the start of the file to the initial byte to be read, and the number of bytes to be read and returned to the reader. write binary: this command is used to insert data into a transparent ef on the card. this command can be used to set a series of bytes in the ef (i.e. set selected bits within a specified byte to a value of 1), clear a series of bytes or perform a write of a series of bytes in the ef.
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 25/40 update binary: a reader-side application can utilize this command to directly erase and store a contiguous sequence of bytes in a transparent ef on the card. it effectively functions as a write command i.e. a string of bytes provided in the command are written into the ef on the card. the input parameters consist of an offset pointer from the start of the file as well as the number of bytes to be written. erase binary: the erase binary command is used to clear bytes within a transparent ef on a card. similarly to the previous commands, the input parameters comprise an offset from the start of the ef to the segment of bytes to be erased as well as the number of bytes to be erased. read record: this command is used to read and return the contents of one or more records in an ef on a card. unlike the previous command, the ef for the read record command must be a record- oriented file. if it is applied to a transparent ef, the command will abort and an error will be returned to the reader. the following may be returned from this command, depending on the input parameters: a specified record all the records from the beginning of the file to a specific record all the records from a specific record to the end of the file write record: this command is used to write a record into a record-oriented ef. as with the write binary command, this command can be used to write a record into an ef, set or clear specific bits within a specific record in an ef. append record: the append record command is used to add a record to the end of a linear, record-oriented ef or to write the first record to a cyclic, record-oriented ef on a card. update record: this command writes a specific record into a record-oriented ef on a card. as with the update binary command, the old record is erased and the new one is written into the ef. get data: this command reads and returns the contents of a data object stored with in the file system on the card. the get data command is card-specific as the definition of a data object varies between different cards. put data:
7 iso 7816-3 - electronic signal and transmission protocol AN2284 26/40 this command (as the name suggests) puts information into a data object on the card. as with the previous command, this is a card-specific command. 7.2.4 the security api each component of the file system on a smart card has an associated list of access properties. the access properties ensure that only authorized parties are allowed to access the particular component of the file system. th e authentication can be simple, such as requiring the reader to provide a predefined personal identification number (pin). alternatively, it may be more complicated, such as requiring the reader to prove that it possess some shared secret with the card (e.g. a key) by encrypting or decrypting a string of bytes provided by the card. a few of the functions provided by the security api are briefly discussed below. verify: this command is sent by the application on the reader side to the security system on the card. its purpose is to convince the card that the reader knows a password maintained by the card in order to restrict access to sensitive information stored on the card. the password-type information may be associated with a specific file or to some or all of the file hierarchy. if the verify command fails i.e. the reader provides an incorrect password, an error is returned to the reader. internal authenticate: this command allows the card to authenticate itself to the reader by proving that it possesses a secret key shared with the reader. the reader application software first generates a random number and encrypts it with some algorithm known to both card and reader. this constitutes a challenge to the card. the card then decrypts this challenge with the secret key (that is stored on the card) and sends the resulting data back to the reader. if the data received by the reader matches the random number that it generated then the reader application software is assured of the identity of the card. external authenticate: this command is used in conjunction with the get challenge command to enable the reader application software to authenticate itself to the card. the reader receives challenge data (a random number) from the card and encrypts it with a secret key. this is then sent to the card using the external authenticate command. the card decrypts the data and compares it to the random number that it generated in the previous get challenge command. if there is a match, then the card is assured of the identity of the reader application. get challenge: this command is sent by the reader to the card. its purpose is to provide the reader application with a random number generated by the smart card. as previously described, this number is used in the external authenticate command. manage channel:
AN2284 7 iso 7816-3 - electronic signal and transmission protocol 27/40 the manage channel command is used by the reader application to open and close the logical communication channels between it and the card. initially the card opens a basic communication channel by establishing an application-level protocol with the reader application through the completion of an atr sequence. this channel is then used to open or close additional logical channels through the manage channel command. envelope: this command supports the use of secure messaging using the t=0 protocol. it enables an apdu to be encrypted and then incorporated into the envelope command's data section (of its apdu). the apdu processor on the card can then extract and execute the command. get response: as with the previous command, the get response command allows the use of the t=0 protocol for transferring apdus. the case 4 type of apdu cannot be supported by the t=0 protocol i.e. it is not possible to send a block of data to the card and then receive a block of data in return. so when using the t=0 protocol, the initial command results in a response which indicates that more data is waiting to be sent by the card. the get response command is then used to retrieve this data.
8 sc reader lib: description AN2284 28/40 8 sc reader lib: description the user may access a smart card using directly the application layer. it allows to send/receive adpu commands to/from the smart card using the following user interface: u8 sc_handler (u8 sc_action, u8 *data_to_sc, u8 data_lenght, u8 p1, u8 p2, sc_state *sc_state, u16 *cmdstatus, u8 *data_from_sc) 8.1 sc parameters 8.1.1 sc_action the user has to specify which action he wants to perform on the smart card using the sc_action parameter. actions available are: data area management commands - sc_select_file - sc_get_responce - sc_status - sc_update_binary - sc_read_binary - sc_write_binary - sc_update_record - sc_read_record administrative commands - sc_create_file safety management commands - sc_verify - sc_change - sc_unblock - sc_external_auth - sc_get_challenge answer to reset commands - sc_get_a2r 8.1.2 sc_state sc_state informs the user about the smart card state and allows to power off the smart card. the smart card states are: - sc_power_off - sc_power_on
AN2284 8 sc reader lib: description 29/40 - sc_reset_low - sc_reset_high - sc_active - sc_active_on_t0 sc_power_off no power is provided to the smart card (vcc=0); the str7 smart card peripheral is disabled. no clock is provided to smart card. sc_power_on the smart card peripheral is enabled and initialized; no power is provided to the smart card; no clock is provided to the smart card. sc_reset_low and sc_reset_high in sc_reset_low rst, the pin on the smart card (pin 2) is hold to zero (rst=0). vcc=5v is provided to the smart card; clock clk is provided to the smart card. answer to reset (atr) procedure begins. the reader waits for an atr frame from the smart card. if no answer is received, the reader forces reset pin rst high (rst=1) and waits again for an answer to reset in the sc_reset_high state. sc_active if an answer to reset is received, the reader goes in active state and decodes the atr frame. it returns information about the used protocol. sc_active_on_t0 if the used protocol is t0, the reader goes in sc_active_on_t0 state and allows to send commands to the smart card.
8 sc reader lib: description AN2284 30/40 figure 10. state machine for smart card operation 8.1.3 cmdstatus cmdstatus retrieves the smart card status response related to the selected adpu command according the iso7816 standard. possible card answers: - sc_ef_selected - sc_df_selected - sc_op_terminated - sc_df_existent - sc_ef_not_selected - sc_id_not_found - sc_memory_problem - sc_wrong_header - sc_wrong_p1_p2 - sc_wrong_lc_le 8.2 how to send adpu commands to sc a detailed description on how to use the sc_handler( ) function to send the adpu command to sc and retrieve the card response is found below. power off power on reset low active active on t0 reset high user force power on atr received with rst=0 no atr received no atr received atr received with rst=1 t0 protocol no t0 protocol user power off smart card
AN2284 8 sc reader lib: description 31/40 8.2.1 sc_select_file sc_handler (sc_select_file, (u8 *) filename, null_param, null_param, &sc_state, &u16_status, void_pointer) input - filename it contains the 16 bit file identifier and the file type (ef or df). example: 1) userfile={0xaa,0xbb,ef} 2) userdir={0xa0,0x00,df} output - sc_state it returns the current smart card state - u16_status it returns the smart card response to sc_select_file command 8.2.2 sc_get_a2r sc_handler (pvoid, null_param, null_param, null_param, &sc_state, &u16_status, a2rframe) input - no input output - sc_state it returns the current smart card state - u16_status it returns the smart card response to sc_select_file command - a2rframe pointer to an array which contains the smart card atr frame 8.2.3 sc_get_responce sc_handler (pvoid, null_param, null_param, null_param, &sc_state, &u16_status, responce) input - no input output - sc_state it returns the current smart card state - u16_status
8 sc reader lib: description AN2284 32/40 it returns the smart card status response to sc_get_response command - response it returns the smart card data response to sc_get_response command 8.2.4 sc_read_binary sc_handler (sc_raed_binary, filename, byte_number, offset_high, offset_low, &sc_state, &u16_status, responce) input - filename it contains the 16 bit file identifier example: 1) userfile = {0xaa,0xa0} - byte_number it contains size area (in byte) to read (valid only for ef) - offset_high, offset_low it contains the 16 bits offset where reading begins output - sc_state it returns the current smart card state - u16_status it returns the smart card status response to sc_read_binary command - response - it returns the smart card data to read 8.2.5 sc_create_file sc_handler ( sc_create_file, filename, filesize, null_param, null_param, &sc_state, &u16_status,pvoid ) input - filename it contains the 16 bit file identifier and the file type (ef or df). example: 1) userfile = {0xaa,0xa0, ef} - filesize it contains size area (in byte) to allocate (valid only for ef) output - sc_state it returns the current smart card state
AN2284 8 sc reader lib: description 33/40 - u16_status - it returns the smart card status response to sc_create_file command 8.2.6 sc_update_binary sc_handler ( sc_update_binary, fileupdate, bytenumber, offset_high, offset_low, &sc_state, &u16_status, pvoid ) input - fileupdate pointer to the array of byte to write - bytenumber it contains size area (in byte) to read (valid only for ef). - offset_high, offset_low it contains the 16 bits offset where writing begins output: - sc_state it returns the current smart card state - u16_status it returns the smart card status response to sc_update_binary command 8.2.7 sc_verify sc_handler ( sc_verify, password, pwd_lenght, null_param, sc_verify_pinx, &sc_state, &u16_status, pvoid ) input - password pointer to the 8 byte array containing the pin to verify - pwd_lenght it contains a constant fixed to 8. - sc_verify_pinx it indicates the pin to verify (x=1?4). output - sc_state it returns the current smart card state - u16_status it returns the smart card status response to sc_update_binary command.
9 sc reader lib: parity error management AN2284 34/40 9 sc reader lib: parity error management in the t=0 protocol error handling is performed on each byte by looking at the parity bit. if the actual parity bit does not correspond to the parity of the transmitted data, then an error must have occurred; the receiving side signals that it requires the byte to be retransmitted in the case of detecting a parity error. this is done by holding the i/o line low (normally the i/o line is set high preceding the transfer of a byte). when the transmitting side detects this, it resends the byte that was previously received in error. 9.1 data sent from card to reader str71xx is able to detect via hw a parity error on a received data pulling down the data line during the stop bit. 9.2 data sent from reader to card vice versa if the smart card pulls down the i/o line to signal that a parity error occurred, the user needs to check it by sw in order to resend the last data. the sc library uses the uart_bytesend_scirq funtion (sc.c file) in conjunction with: - sc_parityerrorirqhandler(void) (device_scr.c file) funtion which checks if the parity error occurs and manages it. each time the uart_bytesend_scirq function is called it: - sends a byte - enables the reception (uart_rxconfig(uart1 ,enable)) in order to capture the transmitted byte - enables the frame error interrupt source after a byte is sent from the microcontroller to the card, the smart card peripheral captures the data sent on the i/o line. if a parity error is detected from the card, the i/o line is pulled down during the stop bits. a frame error event occurs and the related irq event invokes the sc_parityerrorirqhandler(void) function. it resends the last data and disables the irq source.
AN2284 10 sc reader example 35/40 10 sc reader example an example is provided in conjunction with the smart card library in order to help the user to develop its custom application. the example provides simple operations with a smart card such as file system exploration, files and directories creation, read/write operation on files and pin verify on protected files. 10.1 firmware package description the str71x_sclib_ v02 firmware package contains both the smart card library and the example described in this section. figure 11. shows the directory tree. it consists of three main directories: figure 11. firmware package: directory tree table 7. firmware package: directories description to run the example open the str7_sc.prj file in the smartcard_example/realview directory. 10.2 firmware description three directories have been defined for the smart card directory tree: masterroot[3]= {0x3f,0x00,mf}; privatedir[3]= {0xca,0x00,df}; directory description 71x_lib it contains str71xx lib files scf it contains the scatter files smartcard_example it contains: - main.c, 71x_it.c and 71x_conf.h files - realview project
10 sc reader example AN2284 36/40 publicdir[3]= {0xdd,0x00,df}; figure 12. smart card example: file system description at the end of the example: - the privatedir directory will contains the privatefile file; - the publicdir directory will contains the publicfile file; - the masterroot directory will contains the pwdfile file; the definition and the related file identifier code: privatefile[3]= {0xca,0xa1,ef}; publicfile[3]= {0xcd,0x00,ef}; pwdfile[3]= {0xdd,0xdd,ef}; 10.2.1 smart card start up: answer to reset (a2r) the first action to perform when the reader wants to access a card is the answer to reset procedure. to this aim user has to invoke the sc_handler as follows: sc_state=sc_power_on; while (sc_state!=sc_active_on_t0) sc_handler (sc_get_a2r , pvoid, null_param, null_param, null_param, &sc_state, &u16_status, card); when a card is detected the a2r sequence is genereted, received and decoded. if the recognized protocol is the t0 protocol, the smart card state of the reader will be active (sc_active_on_t0) and the smart card will be available for operations on the file system. 10.2.2 reading a file at aspecified path suppose the specified path reading begins is: masterroot/privatedir/privatefile to reach it the user has to perform the following actions: sc_handler ( sc_select_file , (u8 *) masterroot, null_param, null_param, null_param, &sc_state, &u16_status, card); sc_handler (sc_select_file , (u8 *) privatedir, null_param, null_param, null_param, &sc_state, &u16_status, response);
AN2284 10 sc reader example 37/40 sc_handler (sc_select_file , (u8 *) privatefile, null_param, null_param,null_param, &sc_state, &u16_status, response); once the selected path is accessed, the user has to define the 16bit offset where he wants to read and the number of bytes he wants to read: offset_low=1; offset_high=0; byte_number=3; sc_handler (sc_read_binary , (u8 *) privatefile, byte_number, offset_high, offset_low, &sc_state, &u16_status, response); 10.2.3 writing a file at a specified path suppose the user wants to write the publicfile in the masterroot . he has to define the 16bits offset where he wants to read and the number of bytes he wants to read. in the example, fileparamst array will store the sequence the user wants to write. offset_low=1; offset_high=0; byte_number=18; fileparamst[40]="smartcarddemo"; sc_handler (sc_update_binary , (u8 *) fileparamst , byte_number, offset_high,offset_low, &sc_state, &u16_status, response); 10.2.4 creating a file at a specified path suppose the user wants to create a file in the masterroot . sc_handler (sc_select_file , (u8 *) masterroot , null_param, null_param, null_param, &sc_state, &u16_status, card); sc_handler (sc_create_file , (u8 *) publicfile , 20,null_param, null_param, &sc_state, &u16_status, response); where 20 is the size (in byte) of publicfile. 10.2.5 creating a file at a specified path with pin protection suppose the user wants to create a file in the masterroot/publicdir directory. sc_handler (sc_select_file , (u8 *) masterroot , null_param, null_param, null_param, &sc_state, &u16_status, card); sc_handler (sc_create_file , (u8 *) publicdir , null_param, null_param,null_param, &sc_state, &u16_status, response); sc_handler (sc_select_file , (u8 *) publicdir , null_param, null_param,null_param, &sc_state, &u16_status, response);
10 sc reader example AN2284 38/40 pwdfile will be created with pin1 enabled for read and write access: sc_handler (sc_create_file , (u8 *) pwdfile , 40, sc_pin1_w | sc_pin1_r, null_param, &sc_state, &u16_status, response); if the user verifies pin1 for read/write operations, pwdfile can be accessed. when the user tries to verify a pin, the related pwd has to be specified. in this example the following 8byte password is associated with pin1: pwd= {"1", "1", "1", "1", "1", "1", "1", "1"} sc_handler (sc_verify , (u8 *) pwd, pwd_lenght, null_param,sc_verify_pin1, &sc_state, &u16_status, response); after that he can be selected: sc_handler (sc_select_file , (u8 *) pwdfile , null_param, null_param,null_param, &sc_state, &u16_status, response);
AN2284 11 revision history 39/40 11 revision history date revision changes 05-jan-2006 1.0 first edition
11 revision history AN2284 40/40 i nformation furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the con sequence s o f use of such information nor for any infringement of patents or other rights of third parties which may result from its use. n o license is grante d b y implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicatio n are subje ct t o change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics produ cts are n ot a uthorized for use as critical components in life support devices or systems without express written approval of stmicroelectron ics. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners ? 2006 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - belgium - brazil - canada - china - czech republic - finland - france - germany - hong kong - india - israel - ital y - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - united states of america www.st.com


▲Up To Search▲   

 
Price & Availability of AN2284

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X